package com.yunhai.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunhai.common.dto.GlobalException;
import com.yunhai.common.dto.PaginationVo;
import com.yunhai.common.utils.ObjectUtil;
import com.yunhai.common.utils.ReflectionCopy;
import com.yunhai.device.mapper.ProductCategoryMapper;
import com.yunhai.device.service.ProductCategoryService;
import com.yunhai.model.entity.dms.DmsProductCategoryEntity;
import com.yunhai.model.vo.dms.DmsProductCategoryQuery;
import com.yunhai.model.vo.dms.DmsProductCategoryVo;
import org.apache.commons.beanutils.PropertyUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {

    @Resource
    private ProductCategoryMapper productCategoryMapper;

    /**
     * 获取 ProductCategory 分页列表
     */
    @Transactional
    public IPage getProductCategoryPagination(PaginationVo paginationVo, DmsProductCategoryQuery dmsProductCategoryQuery ) throws Exception {

        IPage page = new Page( paginationVo.getPageIndex(), paginationVo.getPageSize() );
        LambdaQueryWrapper<DmsProductCategoryEntity> lqs = new LambdaQueryWrapper();
        lqs.likeRight( dmsProductCategoryQuery.getName()!=null, DmsProductCategoryEntity::getName, dmsProductCategoryQuery.getName() );
        lqs.orderByDesc( DmsProductCategoryEntity::getCreateTime );
        productCategoryMapper.selectPage(page, lqs);

        List<DmsProductCategoryEntity> record = page.getRecords();
        if( !ObjectUtil.isNotEmptyList(record)){
            return page;
        }
        return page;
    }


    /**
     * 获取 ProductCategory 所有数据
     *
     */
    @Transactional
    public List<DmsProductCategoryEntity> getProductCategoryAllList( DmsProductCategoryQuery dmsProductCategoryQuery ) throws Exception {

        LambdaQueryWrapper<DmsProductCategoryEntity> lqs = new LambdaQueryWrapper();
        List<DmsProductCategoryEntity> list = productCategoryMapper.selectList(lqs);
        return list;
    }


    /**
     * 获取 ProductCategory 详情
     */
    @Transactional
    public DmsProductCategoryEntity getProductCategoryDetail( Long id) throws Exception {

        DmsProductCategoryEntity dmsProductCategoryEntity = productCategoryMapper.selectById(id);
        return dmsProductCategoryEntity;
    }


    /**
     * 新增 ProductCategory
     * */
    @Transactional
    public void addProductCategory(DmsProductCategoryVo dmsProductCategoryVo) throws Exception {

        DmsProductCategoryEntity dmsProductCategoryEntity = new DmsProductCategoryEntity();
        ReflectionCopy.copyProperties( dmsProductCategoryEntity, dmsProductCategoryVo );
        productCategoryMapper.insert(dmsProductCategoryEntity);
        return;
    }


    /**
     * 更新 ProductCategory
     * */
    @Transactional
    public void updateProductCategory( DmsProductCategoryVo dmsProductCategoryVo) throws Exception {

        if( dmsProductCategoryVo.getId() == null ){
            throw GlobalException.gen(10021, "id不存在");
        }
        DmsProductCategoryEntity dmsProductCategoryEntity = new DmsProductCategoryEntity();
        ReflectionCopy.copyProperties( dmsProductCategoryEntity, dmsProductCategoryVo );
        productCategoryMapper.updateById(dmsProductCategoryEntity);
        return;
    }

    /**
     * 删除 ProductCategory
     * */
    @Transactional
    public void batchRemoveProductCategory( List<Long> ids ) throws Exception {

        if( !ObjectUtil.isNotEmptyList(ids) ){
            throw GlobalException.gen(10011, "要删除的id不存在");
        }
        productCategoryMapper.deleteBatchIds(ids);
    }

}